package ym.controller.back.repair;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.RepairReport;
import ym.service.RepairReportService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台维修报备
 */
@RestController
@CrossOrigin
@RequestMapping("/back/repairReport")
public class RepairReportController {
    @Autowired
    private RepairReportService repairReportService;

    /**
     * 新增
     *
     * @param repairReport
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/repair')")
    public BaseResult add(RepairReport repairReport) {
        repairReportService.add(repairReport);
        return BaseResult.ok();
    }

    /**
     * 修改
     *
     * @param repairReport
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(RepairReport repairReport) {
        repairReportService.update(repairReport);
        return BaseResult.ok();
    }

    /**
     * 删除
     *
     * @param repairReportId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer repairReportId) {
        repairReportService.delete(repairReportId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询
     *
     * @param repairReportId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer repairReportId) {
        RepairReport repairReport = repairReportService.findById(repairReportId);
        return BaseResult.ok(repairReport);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<RepairReport> repairReports = repairReportService.findAll();
        return BaseResult.ok(repairReports);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/repair')")
    public BaseResult search(String search, int page, int size) {
        Page<RepairReport> repairReportPage = repairReportService.search(search, page, size);
        return BaseResult.ok(repairReportPage);
    }
}
